package com.shop.loan.proxy;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.view.UserByOrderEnd;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.DataProxy;
import xyz.erupt.annotation.query.Condition;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class UserByOrderEndProxy implements DataProxy<UserByOrderEnd> {

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    Util util;

    @Override
    public String beforeFetch(List<Condition> conditions) {
        conditions.clear();
        return DataProxy.super.beforeFetch(conditions);
    }

    @Override
    public void afterFetch(Collection<Map<String, Object>> list) {
        list.clear();
        String pageIndexStr = util.getSearchParam("pageIndex", 1);
        String pageSizeStr = util.getSearchParam("pageSize", 1);
        String cardName = util.getSearchParam("cardName", 1);
        String cardNum = util.getSearchParam("cardNum", 1);
        String mobile = util.getSearchParam("mobile", 1);
        String channelIdStr = util.getSearchParam("channel", 1);
        String machineStatusStr = util.getSearchParam("machineStatus", 1);
        String userWillStr = util.getSearchParam("user_will", 1);
        pageIndexStr = pageIndexStr == null ? "1" : pageIndexStr;
        pageSizeStr = pageSizeStr == null ? "10" : pageSizeStr;

        UserByMybatis user = new UserByMybatis();
        user.setCardName(cardName);
        user.setCardNum(cardNum);
        user.setMobile(mobile);
        if (StringUtils.hasLength(channelIdStr)) {
            user.setChannelId(Long.parseLong(channelIdStr));
        }
        if (StringUtils.hasLength(userWillStr)) {
            user.setUserWill(Integer.parseInt(userWillStr));
        }
        if (StringUtils.hasLength(machineStatusStr)) {
            user.setMachineStatus(Boolean.parseBoolean(machineStatusStr));
        }else{
            user.setMachineStatus(true);
        }

        PageHelper.startPage(Integer.parseInt(pageIndexStr), Integer.parseInt(pageSizeStr));
        Page<UserByMybatis> userByMybatisList = loanOrderByMybatisMapper.selectByUserEnd(user);
        if (userByMybatisList == null || userByMybatisList.size() == 0) {
            return;
        }
        for (UserByMybatis userByMybatis : userByMybatisList) {
            userByMybatis.setPageIndex(userByMybatisList.getPageNum());
            userByMybatis.setPageSize(userByMybatisList.getPageSize());
            userByMybatis.setTotal(userByMybatisList.getTotal());
            userByMybatis.setTotalPage(userByMybatisList.getPages());

            Map map = JSON.parseObject(JSON.toJSONString(userByMybatis), Map.class);

            if (userByMybatis.getCreateTime() != null) {
                map.put("createTime", DateUtil.date(userByMybatis.getCreateTime()).toString());
            }

            if (userByMybatis.getFinishTime() != null) {
                map.put("finishTime", DateUtil.date(userByMybatis.getFinishTime()).toString());
            }

            map.put("userInfo", "姓名：" + userByMybatis.getCardName() + "\n 手机号：" + userByMybatis.getMobile() + "\n 身份证：" + userByMybatis.getCardNum());

            list.add(map);
        }
    }


}
